package hn.cch.spring_boot_security_rest.message;

import hn.cch.spring_boot_security_rest.constant.StatusCode;
import hn.cch.util.JsonUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;


@ApiModel(value="接口返回模板",description="适用于本系统所有接口返回")
public class Response<T> implements Serializable {

    @ApiModelProperty(value = "状态码", example="1000")
    protected String statusCode;
    @ApiModelProperty(value = "提示语", example="成功")
    protected String promptInfo;

    @ApiModelProperty(value = "返回数据", example="null")
    protected T data;

    @ApiModelProperty(value = "请求编号", example="124567890abcdefghijklmnopqrstuvwsyzf")
    protected String requestId;
    @ApiModelProperty(value = "接口耗时", example="0.123456789ms")
    protected String spendTime;

    public static <T> Response<T> success(T t) {
        Response response = new Response();
        response.setStatusCode(StatusCode.SUCCESS.getCode());
        response.setPromptInfo(StatusCode.SUCCESS.getInfo());
        response.setData(t);
        return response;

    }

    public static Response failure(StatusCode statusCode) {
        Response response = new Response();
        response.setStatusCode(statusCode.getCode());
        response.setPromptInfo(statusCode.getInfo());
        return response;
    }

    /**
     * 处理未知错误传入错误信息
     * @param promptInfo
     * @return
     */
    public static Response failure(String promptInfo) {
        Response response = new Response();
        StatusCode statusCode = StatusCode.FAILURE;
        if (promptInfo != null){
            response.setPromptInfo(promptInfo);
        }else {//如果错误信息为空就赋值返回码的中文提示
            response.setPromptInfo(statusCode.getInfo());
        }
        response.setStatusCode(statusCode.getCode());
        return response;
    }


    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getPromptInfo() {
        return promptInfo;
    }

    public void setPromptInfo(String promptInfo) {
        this.promptInfo = promptInfo;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }


    public String getSpendTime() {
        return spendTime;
    }

    public void setSpendTime(String spendTime) {
        this.spendTime = spendTime;
    }

    @Override
    public String toString() {
        return JsonUtil.toStringJackson(this);
    }
}
